package com.mesd.killer.ui.tools;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URI;

import javax.swing.JEditorPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;

import com.mesd.killer.GameMainUI3;

public class MyJLabel extends JTextPane {

	private static final long serialVersionUID = -5761504877839572931L;
	private Color enteredColor;
	private Color exitedColor;
	private URI url = null;

	public MyJLabel() {
		this("");
	}

	public MyJLabel(String text) {
		this(text, Color.RED, Color.BLUE);
	}

	public MyJLabel(String text, URI url) {
		this(text, url, Color.RED, Color.BLUE);
	}

	public MyJLabel(String text, Color entered, Color exited) {
		this.enteredColor = entered;
		this.exitedColor = exited;
		this.initGUI();
		this.addListener();
		this.setFont(FontUtil.getDefaultFont(16));
		this.setText(text);
	}

	public MyJLabel(String text, URI url, Color entered, Color exited) {
		this.enteredColor = entered;
		this.exitedColor = exited;
		this.url = url;
		this.initGUI();
		this.addListener();
		this.setFont(FontUtil.getDefaultFont(12));
		this.setText(text);
	}

	public Color getEnteredColor() {
		return enteredColor;
	}

	public void setEnteredColor(Color enteredColor) {
		this.enteredColor = enteredColor;
	}

	public Color getExitedColor() {
		return exitedColor;
	}

	public void setExitedColor(Color exitedColor) {
		this.exitedColor = exitedColor;
	}

	public URI getUrl() {
		return url;
	}

	public void setUrl(URI url) {
		this.url = url;
	}

	private void initGUI() {
		this.setOpaque(false);
		this.setBorder(null);
		this.setEditable(false);
		this.setForeground(exitedColor);
		this.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}

	private void addListener() {
		this.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {

				if (!isEnabled()) {
					return;
				}

				setForeground(enteredColor);
				setUnderbind(getOwn());
				String str = getText();
				setText("");
				setText(str);
			}

			public void mouseExited(MouseEvent e) {
				if (!isEnabled()) {
					return;
				}
				setForeground(exitedColor);
				setUnderbind(getOwn());
				String str = getText();
				setText("");
				setText(str);
			}

			public void mouseReleased(MouseEvent e) {
				setForeground(exitedColor);
				if(url == null || "".equals(url))
				{
					e.consume();
					return;
				}
				DeskTopBrowser.showUrlDesktopBrowse(url);
			}
		});
	}

	private MyJLabel getOwn() {
		return this;
	}

	private void setUnderbind(JEditorPane editor) {
		StyledEditorKit kit = getStyledEditorKit(editor);
		MutableAttributeSet attr = kit.getInputAttributes();
		boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
		SimpleAttributeSet sas = new SimpleAttributeSet();
		StyleConstants.setUnderline(sas, underline);
		setCharacterAttributes(editor, sas, false);
	}

	private void setCharacterAttributes(JEditorPane editor, AttributeSet attr,
			boolean replace) {
		int p0 = editor.getSelectionStart();
		int p1 = editor.getSelectionEnd();
		if (p0 != p1) {
			StyledDocument doc = getStyledDocument(editor);
			doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
		}
		StyledEditorKit k = getStyledEditorKit(editor);
		MutableAttributeSet inputAttributes = k.getInputAttributes();
		if (replace) {
			inputAttributes.removeAttributes(inputAttributes);
		}
		inputAttributes.addAttributes(attr);
	}

	private StyledDocument getStyledDocument(JEditorPane e) {
		Document d = e.getDocument();
		if (d instanceof StyledDocument) {
			return (StyledDocument) d;
		}
		throw new IllegalArgumentException("document must be StyledDocument");
	}

	private StyledEditorKit getStyledEditorKit(JEditorPane e) {
		EditorKit k = e.getEditorKit();
		if (k instanceof StyledEditorKit) {
			return (StyledEditorKit) k;
		}
		throw new IllegalArgumentException("EditorKit must be StyledEditorKit");
	}

	public int getStringWidth() {
		int w = this.getFontMetrics(this.getFont()).stringWidth(this.getText());
		return w;
	}

}
